package com.temp.pdf;

import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

//import com.itextpdf.html2pdf.HtmlConverter;

public class HtmlPdfMain {
    public static void main(String[] args) {
        try {
            //html to pdf 控制生产pdf 大小
            //https://segmentfault.com/q/1010000043107862/a-1020000043107864
            File file = new File("C:\\Users\\Administrator\\Desktop\\word\\test.html");
            File outFile = new File("C:\\Users\\Administrator\\Desktop\\word\\" + System.currentTimeMillis() + ".pdf");
            // 保存修改后的文档
            List<String> lines = Files.readAllLines(Paths.get(file.getAbsolutePath()), Charset.forName("UTF-8"));
            StringBuilder inTxt = new StringBuilder();
            for (String line : lines) {
                inTxt.append(line);
            }
            HtmlConverter.convertToPdf(inTxt.toString(), new PdfWriter(outFile));
            System.out.println("PDF created successfully.");

            System.out.println("转换为pdf完成");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
